//获取应用实例
const app = getApp();
var util=require("../../utils/util.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    index:0,
    array:['男', '女'],
    objectArray: [
      {
        id: 0,
        name: '男'
      },
      {
        id: 1,
        name: '女'
      },
    ],
  
    region: ["省、","市、","区"],
    avatarUrl:"",
    userInfo:"",
    date:"出生日期",
    end_date:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    //获取当前日期
    var end_time = util.formatTime(new Date());
    that.setData({
      avatarUrl: app.globalData.userInfo.avatarUrl,
      userInfo: app.globalData.userInfo,
      end_date: end_time
    })
    //请求个人信息
    wx.request({
      url: 'https://yangyh.applinzi.com/getUserInfo.php',
      data: {
        "openId": app.globalData.openId
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "POST",
      success: function (res) {
        console.log(res);
        if (res.data.error_id==0){
          var userInfo=res.data.info;
          that.setData({
            region: [userInfo.province,userInfo.city,userInfo.area],
            index: userInfo.sex=='男' ? 0 : 1 ,
            date:userInfo.born_date
          })
        }
      }
    })
  },
  bindRegionChange:function(res){
    this.setData({
      'region':res.detail.value
    });
  },
  bindDateChange:function(ea){
    this.setData({
      'date': ea.detail.value
    });
  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  formSubmit:function(e){
    var that = this.data;
    var sex = that.objectArray[that.index].name;
    var verify_con=true;
  
    if(that.region['0']=='省、'){
      wx.showModal({
        title: '请选择家乡地址',
        content: '温馨提醒：选择家乡地址才能查看老乡哦',
        showCancel: true,
      })
      verify_con = false;
    }
    if (verify_con){
      console.log(sex);
      wx.request({
        url: 'https://yangyh.applinzi.com/location.php',
        data: {
          "sex": sex,
          "area": that.region['2'],
          "city": that.region['1'],
          "province": that.region['0'],
          "born_date": that.date,
          "openId": app.globalData.openId
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: "POST",
        success: function (res) {
          console.log(res)
          wx.showModal({
            title: '保存成功！',
            content: '快看你的老乡都在哪儿',
            confirmText:"好的",
            cancelText:"稍后再看",
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/location/location',
                })
              } else if (res.cancel) {
                wx.navigateBack({
                  url: '/pages/personal/personal'
                })
              }
            }
          })
        }
      })
    }
      
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})